﻿using NewProcessGuarder.toolFunc;
using System;
using System.ComponentModel;


namespace NewProcessGuarder.core
{
    [Serializable]
    public class AppGuarder : IAppGuarder, INotifyPropertyChanged
    {
        #region 属性

        private string name;
        public string Name
        {
            get { return name; }
            set {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        private string appPath;
        public string AppPath
        {
            get { return appPath; }
            set {
                appPath = value;
                OnPropertyChanged("AppPath");
            }
        }

        private int interval = 1000 * 60 * 2;
        public int Interval
        {
            get { return interval; }
            set {
                interval = value;
                OnPropertyChanged("Interval");
            }
        }

        private DateTime? beginTime;
        public DateTime? BeginTime {
            get {
                return beginTime;
            }
            set {
                beginTime = value;
                OnPropertyChanged("BeginTime");
            }
        }

        private DateTime? endtime;
        public DateTime? EndTime {
            get {
                return endtime;
            }
            set {
                endtime = value;
                OnPropertyChanged("EndTime");
            }
        }

        private string killOtherPath;
        public string KillOtherPath {
            get {
                return killOtherPath;
            }
            set {
                killOtherPath = value;
                OnPropertyChanged("KillOtherPath");
            }
        }

        private OperationType operType;
        public OperationType OperType {
            get
            {
                return operType;
            }
            set
            {
                operType = value;
                OnPropertyChanged("OperType");
            }
        }

        [NonSerialized]
        private bool _isRunning = false;
        
        public bool isRunning
        {
            get {
                return _isRunning;
            }
            set {
                _isRunning = value;
                OnPropertyChanged("isRunning");
            }
        }
        
        [field:NonSerializedAttribute()]
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string s)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(s));
            }
        }


        #endregion

        [NonSerialized]
        //public Timer HandlerTimer;
        public System.Timers.Timer HandlerTimer;
        
        public AppGuarder()
        {

        }

        public void Start()
        {
            beginTime = null;
            endtime = null;
            //HandlerTimer = new Timer(new TimerCallback(TimerHandle), null, Timeout.Infinite, Timeout.Infinite);
            //HandlerTimer.Change(500, interval);

            HandlerTimer = new System.Timers.Timer(interval);
            HandlerTimer.Elapsed += HandlerTimer_Elapsed;
            HandlerTimer.Start();
        }

        void HandlerTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (HandlerTimer == null)
                return;

            //可以被重载
            HandlerTimer.Stop();

            TimerHandle(sender);
            //throw new NotImplementedException();

            HandlerTimer.Start();
        }

        public virtual void TimerHandle(object state)
        {
            try
            {
                if (!ProcessFunc.ProcessExists(appPath))
                {
                    ProcessFunc.StartProcessByPath(appPath);
                }
            }
            catch { }
        }

        public void Stop()
        {
            //HandlerTimer.Change(Timeout.Infinite, Timeout.Infinite);
            if (HandlerTimer != null)
            {
                HandlerTimer.Stop();
                HandlerTimer.Elapsed -= HandlerTimer_Elapsed;
                HandlerTimer = null;
            }
        }

        public override string ToString()
        {
            return this.Name;
        }
    }
    
}
